package com.hardroid.workload.ui.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.hardroid.workload.R;
import com.hardroid.workload.utils.Singleton;

public class NewEmployerFragment extends Fragment {

	private EditText nameText;
	private EditText divisionText;
	private EditText unitText;
	private EditText jobText;
	private Spinner genderSpinner;
	private Spinner jabatanSpinner;
	
	private Button cancelButton;
	private Button addButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_new_employer, container,
				false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		nameText = (EditText) getView().findViewById(
				R.id.new_employer_name_text);
		divisionText = (EditText) getView().findViewById(
				R.id.new_employer_division_text);
		unitText = (EditText) getView().findViewById(
				R.id.new_employer_unit_text);
		jobText = (EditText) getView().findViewById(R.id.new_employer_job_text);
		cancelButton = (Button) getView().findViewById(
				R.id.new_employer_cancel_button);
		addButton = (Button) getView().findViewById(
				R.id.new_employer_add_button);
		jabatanSpinner = (Spinner) getView().findViewById(R.id.new_employer_jabatan_spinner);
		genderSpinner = (Spinner) getView().findViewById(R.id.new_employer_gender_spinner);
		
		ArrayAdapter<CharSequence> jabatanAdapter = new ArrayAdapter<CharSequence>(getActivity(), android.R.layout.simple_spinner_item,new String[]{
			"Structural", "Non Structural"
		});
		
		jabatanAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		ArrayAdapter<CharSequence> genderAdapter = new ArrayAdapter<CharSequence>(getActivity(), android.R.layout.simple_spinner_item,new String[]{
			"Male", "Female"
		});
		
		genderAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		
		jabatanSpinner.setAdapter(jabatanAdapter);
		genderSpinner.setAdapter(genderAdapter);

		cancelButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				getActivity().finish();
			}
		});

		addButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				if (!nameText.getText().toString().equals("")
						&& !divisionText.getText().toString().equals("")
						&& !unitText.getText().toString().equals("")
						&& !jobText.getText().toString().equals("")) {
					
					String name = nameText.getText().toString();
					String division = divisionText.getText().toString();
					String unit = unitText.getText().toString();
					String job = jobText.getText().toString();
					boolean isMale = false;
					boolean isStructural = false;
					
					Singleton.currentProject.addResponden(name, division, unit, job,isMale, isStructural);
					
					//commit
					Singleton.employerAdapter.notifyDataSetChanged();					
					boolean saved = Singleton.saveProject(Singleton.currentProject);					
					Toast.makeText(getActivity(), "Successfully add new employer",
							Toast.LENGTH_SHORT).show();
					getActivity().finish();
				} else {
					Toast.makeText(getActivity(), "Data are not valid",
							Toast.LENGTH_SHORT).show();

				}
			}
		});
	}
}
